
// models

//var Patient = function(id, patient_id, dob) {
//    this.id = id;
//    this.patient_id = id;
//    this.dob = dob;
//};




// objects

var patientArray = new Backbone.Collection([]);

// functions

function showPatientInfoHover (patId) {
    console.log(patientArray['"' + patId + '"']);
}

// ajax call to retrieve information
function generateQuery(basic_patient_info_form_id, physical_symptoms_form_id, neuropsy_symptoms_form_id, psychological_test_form_id, diagnostic_test_form_id, icd_information_form_id, prescription_information_form_id) {
    
    if (basic_patient_info_JSON == null && physical_symptoms_form_id == null && neuropsy_symptoms_form_id == null && psychological_test_form_id == null && diagnostic_test_form_id == null && icd_information_form_id == null && prescription_information_form_id == null)
        console.log("no post information");
    else {
    
        var basic_patient_info_JSON = form2ArrayString(basic_patient_info_form_id);
        var physical_symptom_info_JSON = form2ArrayString(physical_symptoms_form_id);
        var neuropsychological_symptom_info_JSON = form2ArrayString(neuropsy_symptoms_form_id);
        var psychological_test_info_JSON = form2ArrayString(psychological_test_form_id);
        var diagnostic_test_info_JSON = form2ArrayString(diagnostic_test_form_id);
        var icd_info_JSON = form2ArrayString(icd_information_form_id);
        var prescription_info_JSON = form2ArrayString(prescription_information_form_id);
          
          
        $.post('../Ajax_Requests/create_query.php',
    		{
    			basic_patient_info: basic_patient_info_JSON,
    			physical_symptom_info: physical_symptom_info_JSON,
    			neuropsychological_symptom_info: neuropsychological_symptom_info_JSON,
    			psychological_test_info: psychological_test_info_JSON,
    			diagnostic_test_info: diagnostic_test_info_JSON,
    			icd_info: icd_info_JSON,
    			prescription_info: prescription_info_JSON
    		},
    		 
    		function(data) {
    		  
                var placeholder;
                var getter = document.getElementById('createQueryForm');
                var infoDiv = document.getElementById('queryResults');
    		    if (infoDiv) {
    		    
    		      placeholder = document.getElementById('queryResults');
    		      placeholder.innerHTML = "";
    		    } else {
    		     
    		      placeholder = document.createElement('div');
    		      placeholder.setAttribute('id', 'queryResults');
    		      
    		      getter.appendChild(placeholder);
    		    }
    		    
                headers = document.createElement('div');
                headers.setAttribute('id', 'resultSchema');
                
                
                info = document.createElement('div');
                info.setAttribute('id', 'resultPatients');
                
                placeholder.appendChild(headers);
                placeholder.appendChild(info);
                
    			var results = eval ('(' + data + ')');
    			
    			if (results['result'] == 'error') {
    			     getter.removeChild(placeholder);
    				alert("Error in processing request: " + results['message']);
    			}
    			else {
                    
                    displayPatients(results['message']['patient_info'], info);
    			}
    		}
    	);
	}
}

// TODO - a list of patients. each list is compacted and can be expanded to show all patient information
function displayPatients (listOfPatientObjects, placeholder) {

    list = document.createElement('div');
    
    for (index in listOfPatientObjects) {
        
        patient_properties = listOfPatientObjects[index];

        patient = new patient_obj();        
        patient.set(
            {
                id: Number(patient_properties.ID),
                cid: patient_properties.PatID,
                dob: patient_properties.DOB,
                date_diagnosed: patient_properties.DateDiagnosed,
                gender: patient_properties.Gender,
                tick_bite: Number(patient_properties.TickBite),
                rash: Number(patient_properties.Rash),
                relationship_status: Number(patient_properties.RelationshipStatus),
                garden_habitat: Number(patient_properties.GardenHabitat),
                hunt_habitat: Number(patient_properties.HuntHabitat),
                occupation_status: Number(patient_properties.OccupationStatus),
                play_habitat: Number(patient_properties.PlayHabitat),
                work_habitat: Number(patient_properties.WorkHabitat)
            });
          
        patientArray.add(patient);
        
        entry = document.createElement('div');
        entry.setAttribute('class', 'patientEntry');
        
        patient_id_container = document.createElement('div');
        patient_id_container.setAttribute('style', 'float: left; width: 20%; height: 100%;text-align: center;font-size: 1.2em;');
        
        patient_id = document.createElement('div');
        patient_id.setAttribute('style', 'padding-top: 25px;');
        patient_id.setAttribute('onmouseover', 'showPatientInfoHover("' + patient.get('pat_id') + '")');
        patient_id.innerHTML = patient.get('pat_id');
        
        patient_id_container.appendChild(patient_id);
        entry.appendChild(patient_id_container);
        
        list.appendChild(entry);
    }

      console.log(patientArray.toJSON());
    placeholder.appendChild(list);
}